package com.hanxiaozhang.jdk8;

import com.hanxiaozhang.commomentity.Student;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 〈一句话功能简述〉<br>
 * 〈流〉
 *
 * @author hanxinghua
 * @create 2022/9/28
 * @since 1.0.0
 */
public class No8Stream {

    public static void main(String[] args) {

        // removeIf
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.removeIf(i -> i % 2 == 0);
        System.out.println(list);

        // allMatch: 判断条件是否匹配所有元素
        System.out.println(list.stream().allMatch(x -> {
            return (x % 2) == 1;
        }));

        // anyMatch：判断条件至少匹配一个元素
        System.out.println(list.stream().anyMatch(x -> {
            return (x % 3) == 1;
        }));

        // 获取流中的第一个元素
        System.out.println(list.stream().findFirst().get());

        // 获取流中任意一个，存在随机性
        System.out.println(list.stream().findAny().get());


        List<Student> studentList = new ArrayList();
        studentList.add(new Student("1","男1"));
        studentList.add(new Student("2","女"));
        studentList.add(new Student("3","女"));
        studentList.add(new Student("4","男2"));
        studentList.add(new Student("5","男1"));
        studentList.add(new Student("6","女"));
        studentList.add(new Student("6","男1"));

        // 按list集合中的某个属性分组，并且分组后，map有序
        LinkedHashMap<String, List<Student>> map =
                studentList.stream().collect
                        (Collectors.groupingBy(Student::getSex,
                                LinkedHashMap::new, Collectors.toList()));

        System.out.println(map);


    }

}
